<template>
  <a-layout class="layout sheep-theme">
    <!-- 侧边栏 -->
    <a-layout-sider
      v-model:collapsed="collapsed"
      :trigger="null"
      collapsible
      class="sidebar"
      width="250"
    >
      <div class="logo" @click="goHome" style="cursor:pointer;">
        <img src="/sheep.png" alt="小羊" class="sheep-logo" />
        <h2>
          <span v-if="!collapsed">小羊记账</span>
        </h2>
      </div>
      <DynamicMenu :collapsed="collapsed" />
    </a-layout-sider>
    <!-- 主内容区域 -->
    <a-layout>
      <!-- 顶部导航栏 -->
      <a-layout-header class="header sheep-header">
        <div class="header-left">
          <a-button type="text" @click="collapsed = !collapsed" class="trigger-btn">
            <MenuFoldOutlined v-if="!collapsed" />
            <MenuUnfoldOutlined v-else />
          </a-button>
          <a-breadcrumb class="breadcrumb">
            <a-breadcrumb-item @click="goHome" style="color:#fff;cursor:pointer;">首页</a-breadcrumb-item>
            <a-breadcrumb-item style="color:#fff;">{{ currentPageTitle }}</a-breadcrumb-item>
          </a-breadcrumb>
        </div>
        <div class="header-right">
          <a-space>
            <a-dropdown>
              <a class="user-dropdown" @click.prevent>
                <img src="/sheep.png" alt="小羊" class="avatar" />
                <span class="username white-text">
                  {{ userStore.user?.nickname || userStore.user?.username || '' }}
                </span>
                <DownOutlined />
              </a>
              <template #overlay>
                <a-menu>
                  <a-menu-item key="profile" @click="goToProfile">
                    <UserOutlined />
                    个人中心
                  </a-menu-item>
                  <a-menu-item key="settings" @click="goToSettings">
                    <SettingOutlined />
                    系统设置
                  </a-menu-item>
                  <a-menu-divider />
                  <a-menu-item key="logout" @click="logout" danger>
                    <LogoutOutlined />
                    退出登录
                  </a-menu-item>
                </a-menu>
              </template>
            </a-dropdown>
          </a-space>
        </div>
      </a-layout-header>
      <!-- 内容区域 -->
      <a-layout-content class="content sheep-content">
        <div class="content-wrapper sheep-content-wrapper">
          <router-view />
        </div>
      </a-layout-content>
    </a-layout>
  </a-layout>
</template>

<script setup>
import { ref, computed } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { message } from 'ant-design-vue'
import {
  SettingOutlined,
  MenuFoldOutlined,
  MenuUnfoldOutlined,
  UserOutlined,
  DownOutlined,
  LogoutOutlined,
} from '@ant-design/icons-vue'
import { useUserStore } from '@/store/user'
import DynamicMenu from './DynamicMenu.vue'

const router = useRouter()
const route = useRoute()
const userStore = useUserStore()
const collapsed = ref(false)

// 根据当前路由获取页面标题
const currentPageTitle = computed(() => {
  const routeTitles = {
    // '/': '首页',
    // '/user': '用户管理',
    // '/role': '角色管理',
    // '/menu': '菜单管理',
  }
  console.log("已经注册的路由",route.path)
  return routeTitles[route.path] || '页面'
})

// 个人中心
const goToProfile = () => {
  message.info('个人中心功能开发中...')
}

// 系统设置
const goToSettings = () => {
  message.info('系统设置功能开发中...')
}

// 退出登录
const logout = async () => {
  try {
    const res = await userStore.logoutAction()
    if (res && res.data && res.data.code === 200) {
    message.success('退出登录成功')
    router.push('/login')
    } else {
      message.error(res?.data?.message || '退出登录失败')
      router.push('/login')
    }
  } catch (error) {
    message.error('退出登录失败')
    router.push('/login')
  }
}

const goHome = () => {
  router.push('/')
}
</script>

<style scoped>
.layout.sheep-theme {
  min-height: 100vh;
  background: linear-gradient(135deg, #7ec8e3 0%, #a8b6ff 60%, #e0e7ff 100%);
}

.sidebar {
  background: #fff !important;
  box-shadow: 2px 0 16px rgba(126, 200, 227, 0.08);
  border-radius: 0 !important;
  position: relative;
  overflow: hidden;
}

.sidebar::before {
  content: '';
  display: block;
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 36px;
  border-top-left-radius: 16px;
  border-top-right-radius: 16px;
  z-index: 1;
}

.sidebar > * {
  position: relative;
  z-index: 2;
}

:deep(.sidebar .ant-menu) {
  background: transparent !important;
  border-inline-end: none;
  color: #222 !important;
  font-size: 16px;
  padding-top: 8px;
}

:deep(.sidebar .ant-menu-item),
:deep(.sidebar .ant-menu-submenu-title) {
  border-radius: 10px;
  margin: 4px 8px;
  background: transparent !important;
  color: #222 !important;
  transition:
    background 0.2s,
    color 0.2s;
}

:deep(.sidebar .ant-menu-item-selected),
:deep(.sidebar .ant-menu-item-active),
:deep(.sidebar .ant-menu-submenu-title-active),
:deep(.sidebar .ant-menu-item:hover),
:deep(.sidebar .ant-menu-submenu-title:hover) {
  background: rgba(255, 255, 255, 0.7) !important;
  color: #222 !important;
  font-weight: normal;
}

:deep(.sidebar .ant-menu-item .anticon),
:deep(.sidebar .ant-menu-submenu-title .anticon) {
  font-size: 18px;
  margin-right: 8px;
  color: #222 !important;
}

.sidebar .ant-menu::-webkit-scrollbar {
  width: 6px;
  background: transparent;
}

.sidebar .ant-menu::-webkit-scrollbar-thumb {
  background: #ffffff;
  border-radius: 4px;
}

.sidebar .ant-menu::-webkit-scrollbar-track {
  background: transparent;
}

.logo {
  height: 72px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #fff !important;
  border-radius: 0 !important;
  margin-bottom: 10px !important;
  margin-top: 2px !important;
  position: relative;
  z-index: 2;
  backdrop-filter: blur(6px) !important;
}

.sheep-logo {
  width: 38px;
  height: 38px;
  margin-right: 10px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.04);
}

.logo h2 {
  margin: 0;
  font-size: 18px;
  color: #3a4660;
  font-weight: bold;
  letter-spacing: 2px;
  display: flex;
  align-items: center;
}

.header.sheep-header {
  background: linear-gradient(90deg, #7ec8e3 0%, #a8b6ff 60%, #e0e7ff 100%);
  padding: 0 24px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  box-shadow: 0 1px 4px rgba(0, 21, 41, 0.04);
}

.header-left {
  display: flex;
  align-items: center;
}

.trigger-btn {
  font-size: 18px;
  margin-right: 16px;
  color: #fff !important;
}

.breadcrumb {
  font-size: 14px;
  color: #fff !important;
}
.breadcrumb :deep(.ant-breadcrumb-separator) {
  color: #fff !important;
}

.header-right {
  display: flex;
  align-items: center;
}

.user-dropdown {
  display: flex;
  align-items: center;
  padding: 8px 12px;
  border-radius: 6px;
  transition: background-color 0.3s;
  cursor: pointer;
}

.avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  object-fit: cover;
  background: #fff;
  border: 2px solid #eee;
  box-shadow: 0 2px 8px rgba(0,0,0,0.05);
  margin-right: 8px;
}

.username {
  margin: 0 8px;
  color: #4a6fa5;
  font-weight: 500;
}

.username.white-text {
  color: #fff !important;
}

.content.sheep-content {
  background: linear-gradient(135deg, #7ec8e3 0%, #a8b6ff 60%, #e0e7ff 100%);
  min-height: 100vh;
  padding: 32px 16px 0 16px;
}

.content-wrapper.sheep-content-wrapper {
  background: rgba(255, 255, 255, 0.85);
  border-radius: 18px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.06);
  padding: 32px 24px;
  min-height: 600px;
}

/* 移动端适配 */
@media (max-width: 600px) {
  .layout.sheep-theme {
    min-height: 100vh;
    padding: 0 !important;
  }
  .sidebar {
    width: 64px !important;
    min-width: 48px !important;
    max-width: 80px !important;
    padding: 0 !important;
  }
  .logo {
    padding: 8px 0 8px 0 !important;
    text-align: center;
  }
  .logo h2 {
    font-size: 16px !important;
    margin: 0;
  }
  .sheep-logo {
    width: 32px !important;
    height: 32px !important;
  }
  .header.sheep-header {
    padding: 0 8px !important;
    min-height: 48px !important;
    height: 48px !important;
  }
  .breadcrumb {
    font-size: 13px !important;
  }
  .header-right {
    font-size: 13px !important;
  }
  .avatar {
    width: 28px !important;
    height: 28px !important;
    margin-right: 4px !important;
  }
  .username {
    font-size: 13px !important;
    margin-right: 2px !important;
  }
  .user-dropdown {
    padding: 0 2px !important;
  }
  .content.sheep-content {
    padding: 8px !important;
  }
  .sheep-content-wrapper {
    padding: 0 !important;
  }
}
</style>
